<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
  </div>

  <el-form :inline="true" :model="formInline" class="form-inline">
    <el-form-item label="用户名">
      <el-input v-model="formInline.username" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input placeholder="请输入密码" v-model="formInline.password" show-password></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="login">登陆</el-button>
      <el-button type="primary" @click="initData">登陆状态</el-button>
      <el-button @click="logout">退出登陆</el-button>
    </el-form-item>
  </el-form>

  <el-row>
    <el-col>输出结果为:</el-col>
    <el-col>{{ data }}</el-col>
  </el-row>
</template>

<script>
export default {
  data(){
    return {
      data: null,
      formInline: {
        username: '',
        password: ''
      }
    }
  },
  mounted(){
    this.initData()
  },
  methods: {
    initData(){
      this.$api.get('/api/login').then(res => {
        this.data = JSON.stringify(res.data);
      }).catch(err => {
        console.log(JSON.stringify(err));
        return;
      });
    },
    login(){
      this.$api.post('/api/login',{
        userName: this.formInline.username, 
        password: this.formInline.password
      },{
        headers: {
          "Content-Type":"application/json;charset=utf-8"
        },
        withCredentials : true
      }).then(res => {
        this.data = JSON.stringify(res.data);
      }).catch(err => {
        console.log(JSON.stringify(err));
      });
    },
    logout(){
      this.$api.delete('/api/login').then(res => {
        this.data = JSON.stringify(res.data);
      }).catch(err => {
        console.log(JSON.stringify(err));
      });
    }
  }
}
</script>
<style>

</style>